package com.example.demo.controller;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RequestMapping("/resp")
//@RestController
@Controller
public class ResponseController {
    /**
     * 返回静态页面
     * @return
     */
    @RequestMapping("/index")
    public String index(){
        return "/html/index.html";
    }

    /**
     * 返回数据
     * @return
     */
    @ResponseBody
    @RequestMapping("/indexData")
    public String indexData(){
        return "返回数据";
    }

    /**
     * 返回 html 片段
     * @return
     */
    @ResponseBody
    @RequestMapping("/indexData2")
    public String indexData2(){
        return "<h1>我是中国人</h1>";
    }
    @ResponseBody
    @RequestMapping("/getMap")
    public HashMap<String, String> getMap(){
        HashMap<String, String> map = new HashMap<>();
        map.put("k1", "v1");
        map.put("k2", "v2");
        map.put("k3", "v3");
        map.put("k4", "v4");
        map.put("k5", "v5");
        return map;
    }
    @ResponseBody
    @RequestMapping("/setStatus")
    public String setStatus(HttpServletResponse response){
        response.setStatus(418);
        return "设置状态码";
    }

    @ResponseBody
    @RequestMapping(value = "/setContentType", produces = "application/json")
    public String setContentType(){
        return "{\"OK\":1}";
    }
}
